package com.navi.loaddata.core.job;

import com.navi.loaddata.core.mysqlEntity.EtlConfD;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;

/**
 * Created by Jay on 2018/3/11.
 */
@Component
public class QuartzManage {
    private static final Logger logger = LoggerFactory.getLogger(QuartzManage.class);
    @Resource(name = "scheduler")
    private Scheduler scheduler;

//    public void addJob(QuartzJob job) throws SchedulerException, ClassNotFoundException, IllegalAccessException, InstantiationException {
//        //通过类名获取实体类，即要执行的定时任务的类
//        Class<?> clazz = Class.forName(job.getBeanName());
//        Job jobEntity = (Job)clazz.newInstance();
//        //通过实体类和任务名创建 JobDetail
//        JobDetail jobDetail = newJob(jobEntity.getClass())
//                .withIdentity(job.getJobName()).build();
//        //通过触发器名和cron 表达式创建 Trigger
//        Trigger cronTrigger = newTrigger()
//                .withIdentity(job.getTriggerName())
//                .startNow()
//                .withSchedule(CronScheduleBuilder.cronSchedule(job.getCronExpression()))
//                .build();
//        //执行定时任务
//        scheduler.scheduleJob(jobDetail,cronTrigger);
//    }

    public void addJob(EtlConfD job) throws SchedulerException, ClassNotFoundException, IllegalAccessException, InstantiationException {
        if(getJobDetail(job) != null){
            return;
        }
        //通过类名获取实体类，即要执行的定时任务的类
        Class<?> clazz = Class.forName(job.getClassName());
        Job jobEntity = (Job) clazz.newInstance();
        //通过实体类和任务名创建 JobDetail

        JobDataMap jobDataMap = new JobDataMap();
        jobDataMap.put("etlConf", job);
        JobDetail jobDetail = newJob(jobEntity.getClass())
                .withIdentity(getRealJobName(job), job.getJobGroupName())
                .setJobData(jobDataMap)
                .build();

        //通过触发器名和cron 表达式创建 Trigger
        Trigger cronTrigger = newTrigger()
                .withIdentity(getRealJobName(job), job.getJobGroupName())
                .startNow()
                .withSchedule(CronScheduleBuilder.cronSchedule(job.getCronExpression()))
                .build();
        //执行定时任务
        scheduler.scheduleJob(jobDetail, cronTrigger);
        logger.info("Add job: {}-{}",job.getJobGroupName(), getRealJobName(job));
    }

    public JobDetail getJobDetail(EtlConfD job) throws SchedulerException {
        return scheduler.getJobDetail(new JobKey(getRealJobName(job), job.getJobGroupName()));
    }

//    /**
//     * 更新job cron表达式
//     *
//     * @param quartzJob
//     * @throws SchedulerException
//     */
//    public void updateJobCron(QuartzJob quartzJob) throws SchedulerException {
//
//        TriggerKey triggerKey = TriggerKey.triggerKey(quartzJob.getJobName());
//        CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
//        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(quartzJob.getCronExpression());
//        trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
//        scheduler.rescheduleJob(triggerKey, trigger);
//    }



    /**
     *
     * @param job
     * @throws SchedulerException
     */
    public void deleteJob(EtlConfD job) throws SchedulerException {
        JobKey jobKey = new JobKey(getRealJobName(job), job.getJobGroupName());
        scheduler.deleteJob(jobKey);
    }

//    /**
//     * 恢复一个job
//     *
//     * @param quartzJob
//     * @throws SchedulerException
//     */
//    public void resumeJob(QuartzJob quartzJob) throws SchedulerException {
//        JobKey jobKey = JobKey.jobKey(quartzJob.getJobName());
//        scheduler.resumeJob(jobKey);
//    }

//    /**
//     * 立即执行job
//     *
//     * @param quartzJob
//     * @throws SchedulerException
//     */
//    public void runAJobNow(QuartzJob quartzJob) throws SchedulerException {
//        JobKey jobKey = JobKey.jobKey(quartzJob.getJobName());
//        scheduler.triggerJob(jobKey);
//    }

//    /**
//     * 暂停一个job
//     *
//     * @param quartzJob
//     * @throws SchedulerException
//     */
//    public void pauseJob(QuartzJob quartzJob) throws SchedulerException {
//        JobKey jobKey = JobKey.jobKey(quartzJob.getJobName());
//        scheduler.pauseJob(jobKey);
//    }

    public String getRealJobName(EtlConfD etlConfD) {
        return etlConfD.getJobName() + "_" + etlConfD.getJobNo();
    }

}
